<?php

class ComplainController extends Controller
{
    
	public function actionIndex()
	{
	    $this->redirect(array('Kirim'));
	}
    //jika address Kirim maka dijalankan
    //
    public function actionKirim(){
        $status=0;
        $sms = new kirimSMS;
        $sisaSms= new sisaSMS;
        if (isset($_POST['kirimSMS']))
        {
            if((!empty($_POST['kirimSMS']['isi'])) && (!empty($_POST['kirimSMS']['kepada'])))
            {
                $numRow=cariNomor::model()->find('Name=:Name',array(':Name'=>$_POST['kirimSMS']['kepada']));
                $no= $numRow->Number;
                $status=$this->multiPartOutbox($_POST['kirimSMS']['isi'],$no,$sms,$sisaSms);
                //$this->sendMail();
                /*$sms->TextDecoded=$_POST['kirimSMS']['isi'];
                $sms->DestinationNumber = $no->Number;
                $sms->CreatorID ='pln';
                $sms->save();*/
            }
            else
            {
                $status=3;
            }
            
            $this->render('Kirim', array('sms' => $sms,'status'=>$status));
            
        }else{
        //if(!isset($_POST['kirimSMS'])){
        $this->render('Kirim', array('sms' => $sms,'status'=>$status));
        }
        //echo $id->Auto_increment;
    
        
    }
    //PENGIRIMAN SMS
    //Jika lebih dari 1 halaman akan menggunakan 2 model 
    public function multiPartOutbox($pesan,$kepada,$outBox,$outBoxMulti)
    {
        $gglOrBerhasil=0;
        $idROW=kirimSMS::model()->findBySql("SHOW TABLE STATUS LIKE 'outbox'");
        $id=$idROW->Auto_increment;
        $jmlSMS=ceil(strlen($pesan)/153);
        $pecah=str_split($pesan, 153);
        for ($i=1;$i<=$jmlSMS;$i++)
        {
            $udh="050003A7".sprintf("%02s", $jmlSMS).sprintf("%02s", $i);
            $msg = $pecah[$i-1];
            if ($i == 1)
            {
                $outBox->TextDecoded= $msg;
                $outBox->DestinationNumber = $kepada;
                $outBox->UDH = $udh;
                $outBox->ID = $id;
                $outBox->CreatorID ='pln';
                if($outBox->save())
                {
                    $gglOrBerhasil=1;   
                }
                else
                {
                    $gglOrBerhasil=2;   
                }
                
                
                
            }
            else
            {
                 $outBoxMulti->TextDecoded = $msg;
                 $outBoxMulti->UDH = $udh;
                 $outBoxMulti->ID = $id;
                 $outBoxMulti->SequencePosition = $i;
                 if($outBoxMulti->save())
                 {
                    $gglOrBerhasil=1;   
                 }
                 else
                 {
                    $gglOrBerhasil=2;   
                 }           
                 
            }   
        }
        return $gglOrBerhasil;
    }
    public function sendMail()
    {
        $message = new YiiMailMessage();
 
            $message->setTo(
            array('hans.syahrial@gmail.com'=>'myname'));
            $message->setFrom('hantolang@gmail.com');
            $message->setSubject('hai hans');
            $message->setBody('ok jadi nih kalo masuk');
 
            $numsent = Yii::app()->mail->send($message);
 
            
            $this->refresh();
    }
    

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
    
}